// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.IO;
using System.Xml;
using System.Windows.Forms;

using Microsoft.Win32;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings         
	{
      public string strWebServerUrl;
      public string strMRU_CategoryList;
      public string strCrmSdkUrl;
      public string strCrmUserName = "";
      public string strCrmPassword = "";

		public Settings()
		{
		}

      public void LoadSettings()
      {
         XmlDocument objDoc = new XmlDocument();
         XmlNode     objSettingsNode = null;

         try
         {
            if (!File.Exists("QuoteAdvantageEditorSettings.xml"))
            {
               CreateSettingsFile();
            }

            objDoc.Load("QuoteAdvantageEditorSettings.xml");
            
            objSettingsNode = objDoc.SelectSingleNode("//appSettings/add[@key='MSCRM_WebServer']");
            strWebServerUrl = objSettingsNode.Attributes["value"].Value; 
            
            objSettingsNode = objDoc.SelectSingleNode("//appSettings/add[@key='MRU_CategoryList']");
            strMRU_CategoryList = objSettingsNode.Attributes["value"].Value; 
            
            objSettingsNode = objDoc.SelectSingleNode("//appSettings/add[@key='QuoteAdvantageEditor.CrmSdk.CrmService']");
            strCrmSdkUrl = objSettingsNode.Attributes["value"].Value; 
            
            objSettingsNode = objDoc.SelectSingleNode("//appSettings/add[@key='MSCRM_UserID']");
            strCrmUserName = objSettingsNode.Attributes["value"].Value; 
            
            objSettingsNode = objDoc.SelectSingleNode("//appSettings/add[@key='MSCRM_Password']");
            strCrmPassword = objSettingsNode.Attributes["value"].Value; 
         }
         catch(Exception)
         {
            // File is probably missing
            strWebServerUrl = "http://MY_SERVER";      
         }
      }

      public void SaveSettings()
      {
         XmlDocument objDocument = new XmlDocument();
         XmlNode     objSettingsNode = null;
      
         try
         {
            if (!File.Exists("QuoteAdvantageEditorSettings.xml"))
            {
               CreateSettingsFile();
            }

            objDocument.Load("QuoteAdvantageEditorSettings.xml");

            objSettingsNode = objDocument.SelectSingleNode("//appSettings/add[@key='MSCRM_WebServer']");
            objSettingsNode.Attributes["value"].Value = strWebServerUrl;
            
            objSettingsNode = objDocument.SelectSingleNode("//appSettings/add[@key='MRU_CategoryList']");
            objSettingsNode.Attributes["value"].Value = strMRU_CategoryList;
            
            objSettingsNode = objDocument.SelectSingleNode("//appSettings/add[@key='QuoteAdvantageEditor.CrmSdk.CrmService']");
            strCrmSdkUrl = strWebServerUrl + "/MSCRMServices/2006/CrmService.asmx";
            objSettingsNode.Attributes["value"].Value = strCrmSdkUrl;

            objDocument.Save("QuoteAdvantageEditorSettings.xml");
         }
         catch (Exception ex)
         {
            MessageBox.Show("Error loading QuoteAdvantageEditorSettings.xml. " + ex.Message);
         }
      }

      public void CreateSettingsFile()
      {
         string      strCrmAddress = GetCrmWebAddress();
         string      strQuoteAdvantageFolder = GetQuoteAdvantageWebFolder();
         TextWriter  objFile = new StreamWriter("QuoteAdvantageEditorSettings.xml");

         objFile.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
         objFile.WriteLine(@"<configuration>");
         objFile.WriteLine(@"   <appSettings>");
         objFile.WriteLine(@"      <add key=""MSCRM_WebServer"" value=""" + strCrmAddress + @""" />");
         objFile.WriteLine(@"      <add key=""MRU_CategoryList"" value=""" + strQuoteAdvantageFolder + @"bin"" />");
         objFile.WriteLine(@"      <add key=""QuoteAdvantageEditor.CrmSdk.CrmService"" value=""" + strCrmAddress + @"/MSCRMServices/2006/CrmService.asmx"" />");
         objFile.WriteLine(@"      <add key=""MSCRM_UserID"" value="""" />");
         objFile.WriteLine(@"      <add key=""MSCRM_Password"" value="""" />");
         objFile.WriteLine(@"   </appSettings>");
         objFile.WriteLine(@"</configuration>");
         objFile.Close();
      }

      private string GetQuoteAdvantageWebFolder()
      {
         string strQuoteAdvantageWebFolder = "";
         RegistryKey objKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");

         if (objKey == null)
         {
            strQuoteAdvantageWebFolder = @"C:\inetpub\wwwroot\QuoteAdvantageWeb\";
         }
         else
         {
            string strCrmFolder = objKey.GetValue("WebSitePath").ToString();
            strQuoteAdvantageWebFolder = strCrmFolder + "\\QuoteAdvantageWeb\\";
         }

         return strQuoteAdvantageWebFolder;
      }

 
      //
      private string GetCrmWebAddress()
      {
         string      strWebAddress = "";

         // Locate the CRM web service
         RegistryKey objKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");
         
         if (objKey == null)
         {
            strWebAddress = "http://localhost";
         }
         else
         {
            strWebAddress = objKey.GetValue("ServerUrl").ToString();
            strWebAddress = strWebAddress.Replace("/MSCRMServices", "");
         }

         return strWebAddress;
      }      
  

   } // end class
}
